package com.shph.validation.rule.impl;

import com.shph.validation.Validator;
import com.shph.validation.exception.NotMatchRegexException;
import com.shph.validation.exception.NotRegexFoundException;
import com.shph.validation.rule.IRule;

import java.util.regex.Pattern;

/**
 * 正则匹配校验
 * 被校验的值需要符合指定正则表达式
 */
public class PatternRule implements IRule<String> {
    @Override
    public boolean test(String value, String refValue) {
        if (Validator.isEmpty(refValue)){
            throw new NotRegexFoundException();
        }

        boolean matches = Pattern.matches(refValue, value);
        if (!matches) {
            throw new NotMatchRegexException();
        }
        return true;
    }
}
